package com.apes.enquiry.enquiry.model;

import com.apes.enquiry.enquiry.repository.EnquiryItemRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarLabel;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 类描述：询价单
 *
 * @ClassName Enquiry
 * @Description 描述
 * @Author wushilong
 * @Date 2023/3/15 15:50
 * @Version 1.0
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Enquiry.CONST_TABLE_NAME)
public class Enquiry extends Model {

    public static final String CONST_TABLE_NAME = "sp_mas_enquiry";

    @Id
    @Column(name = "id", length = 10)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "询价单", prefix = "XJ", padding = 8)
    @Comment("编码")
    private String id; //询价单号

    @Column(name = "service_plan_id", length = 32)
    @Comment("服务方案单号")
    private String servicePlanId;

    @Column(name = "vin", nullable = false, length = 32)
    @Comment("车架号")
    private String vin;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vehicle_config", referencedColumnName = "id")
    @Comment("配置车型")
    private CarConfiguration vehicleConfig;

    @Column(name = "auto_brand", nullable = false, length = 32)
    @Comment("车品牌")
    private String autoBrand;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vehicle_label", referencedColumnName = "id")
    @Comment("车厂牌")
    private CarLabel vehicleLabel;

    @Column(name = "car_license", length = 32)
    @Comment("车牌")
    private String carLicense;

    @Column(name = "vendor_code", length = 32)
    @Comment("接入商")
    private String vendorCode;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "car_head", columnDefinition = "CLOB")
    @Comment("车头照片")
    private String carHead;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "car_tail", columnDefinition = "CLOB")
    @Comment("车尾照片")
    private String carTail;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "name_plate", columnDefinition = "CLOB")
    @Comment("铭牌照片")
    private String nameplate;

    @Column(name = "store", length = 32)
    @Comment("门店")
    private String store;//相对于供应商平台是客户

    @Column(name = "store_name", length = 32)
    @Comment("门店名称")
    private String storeName;//相对于供应商平台是客户

    @Column(name = "contacts", length = 32)
    @Comment("联系人")
    private String contacts;

    @Column(name = "contact_phone", length = 32)
    @Comment("联系电话")
    private String contactPhone;

    @Column(name = "address", length = 32)
    @Comment("地址")
    private String address;

    @Column(name = "beInvoice", length = 32)
    @Comment("需要发票")
    private Boolean beInvoice = true;

    @Column(name = "state", length = 32)
    @Comment("状态")
    private String state;//ing(报价中), finish(完成), expired(过期)

    @Column(name = "sum", length = 32)
    @Comment("总数")
    private Integer sum;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "quote_Expire_Date")
    @Comment("报价结果有效期")
    private Date quoteExpireDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "repeatedly_order_date")
    @Comment("推单时间")
    private Date repeatedlyOrderDate;

    @Column(name = "type", length = 12)
    @Comment("询价类型")
    private String type;//parts：配件 tyre：轮胎 refit：改装配件（带安）

    @Column(name = "product_rule", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("带安标记")
    private Boolean productRule = false;

    @Transient
    private Long expireDate;//报价截止时间

    @Transient
    private String expireTime;//报价截止时间

    @Transient
    private String supplierId;//供应商编码

    @Transient
    private String supplierName;//供应商名称

    public void doExpireDate() {
        Date createDate = getCreateDate();
        if(getRepeatedlyOrderDate()!=null){
            createDate = getRepeatedlyOrderDate();
        }
        long time = createDate.getTime();
        List<EnquiryItem> enquiryItems = SpringManager.getBean(EnquiryItemRepository.class).findByEnquiryId(this.id);
        int size = enquiryItems.size();
        this.expireDate = time;
        if (enquiryItems.isEmpty())return;
        SimpleDateFormat sdm = new SimpleDateFormat("HH");
        String atPresent = sdm.format(createDate);
        int onTime = Integer.parseInt(atPresent);//整点
        if (size >= 11){
            if (onTime < 11) {
                time += 1000 * 60 * 60 * 31;
            }else {
                time += 1000 * 60 * 60 * 46;
            }
        }else {
            if (onTime < 14){
                time += 1000 * 60 * 60 * 4;
            }else {
                time += 1000 * 60 * 60 * 19;
            }
        }
        if (onTime < 9){
            int timeLag = 9 - onTime;
            time += 1000L * 60 * 60 * timeLag;
        }
        if (onTime >= 18){
            int timeLag = 33 - onTime;
            time += 1000L * 60 * 60 * timeLag;
        }

        this.expireDate = time;
    }

    public Long getExpireDate() {
        doExpireDate();
        return expireDate;
    }

    public void setExpireDate(Long expireDate) {
        this.expireDate = expireDate;
    }


    @Column(name = "pictures")
    @Comment("图片数组")
    private String pictures;//图片数组

    @Transient
    private String quoteId;//报价单号

    @Transient
    private String offerGroup;//报价组

    public String getExpireTime() {
        SimpleDateFormat sf = new SimpleDateFormat(DateUtil.SHOW_DATETIME_FORMAT);
        expireTime = sf.format(getExpireDate());
        return expireTime;
    }

    public void setExpireTime(String expireTime) {
        this.expireTime = expireTime;
    }
}
